/*	kd:
	Handles all linear fades that happen arbitrarily in the code, such as the
	buttons being lit up gradually. For our sanity, you cannot remove handles
	once added (it would create holes in the arrays). The shop remains
	consistent enough to allow this. */

struct Gcs_LinearFades {
	/*	TODO: Maybe pre-sort into active and dormant lists if it ever happens
		to bottleneck us when we add like 600 faders or whatever... */
	
	Array <float>    m_begin;
	Array <float>    m_end;
	Array <float>    m_current;
	Array <float>    m_t;
	Array <float>    m_step;
	uint             m_size;
	
	/*	This returns a handle to the arrays seen above. Access is everything in
		this pattern. At the cost of juggling a handle like this, you have one
		place in which all these updates are instantly dealt with. */
	
	uint Register () {
		m_begin.	push(0);
		m_end.		push(0);
		m_current.	push(0);
		m_t.		push(0);
		m_step.		push(0);
		m_size++;
		return m_size - 1;
	}
	
	/*	Alternatively, you can clear all and reserve space and see this as a
		glorified array that comes with a processor. */
	
	void ClearAllReserve (uint size) {
		m_begin.	Clear();
		m_end.		Clear();
		m_current.	Clear();
		m_t.		Clear();
		m_step.		Clear();
		
		m_begin.	Reserve(size);
		m_end.		Reserve(size);
		m_current.	Reserve(size);
		m_t.		Reserve(size);
		m_step.		Reserve(size);
		
		for(uint i = 0; i < size; i++) {
			m_begin    [i] = 0;
			m_end      [i] = 0;
			m_t        [i] = 0;
			m_current  [i] = 0;
			m_step     [i] = 0;
		}
		
		m_size = size;
	}
	
	void StepAllFaders () {
		for(uint i = 0; i < m_size; i++) {
			float t = clamp(m_t [i] + m_step [i], 0, 1);
			
			m_current [i] = (1 - t) * m_begin [i] + t * m_end [i];
			m_t       [i] = t;
		}
	}
	
	/*	A usage example might be:
	
		Button init: Register a handle, 'fader'
		Button hovered: Fade(fader, Current(fader), 255, 1.0 / 50);
		Button unhovered: Fade(fader, Current(fader), 0, 1.0 / 30); */
	
	void Fade (uint handle, float begin, float end, float step) {
		if(handle < m_size) {
			m_begin   [handle] = begin;
			m_current [handle] = begin;
			m_end     [handle] = end;
			m_step    [handle] = clamp(step, 0, 1);
			m_t       [handle] = 0;
		}
	}
	
	/*	Continue fading from wherever we left off towards a new end. */
	
	void FadeContinue (uint handle, float end, float step) {
		if(handle < m_size) {
			m_begin   [handle] = m_current [handle];
			m_end     [handle] = end;
			m_step    [handle] = clamp(step, 0, 1);
			m_t       [handle] = 0;
		}
	}
	
	float Current (uint handle) const {
		if(handle < m_size) {
			return m_current [handle];
		}
		
		return 1;
	}
	
	float Interpol (uint handle) const {
		if(handle < m_size) {
			return m_t [handle];
		}
		
		return 1;
	}
	
	/*	TODO: Cache? Seems stupid to do but seriously... */
	float Interval (uint handle) const {
		if(handle < m_size) {
			return m_end [handle] - m_begin [handle];
		}
		
		return 1;
	}
}

/*	kd:
	This interpolates through a pulse function. We don't keep track of pulses,
	because the performance gain would be minimal, and zs is funny. Typically,
	there'll be just one such pulse going on, so this is quite ideal. */

/* struct Gcs_SinglePulse {
	float   m_t;
	float   m_step;
	float   m_current;
	bool    m_active;
	
	void UpdatePulse () {
		if(m_active) {
			m_t = min(m_t + m_step, 1);
			m_current = Gc_Interpol.tpulse_unit(m_t, 0.1);
			
			if(1 <= m_t) {
				m_active = false;
				m_current = 0;
			}
		}
	}
	
	void Pulse (float step) {
		m_t = 0;
		m_step = max(0.0001, step);
	}
	
	float Current () {
		return m_current;
	}
} */